package Vocabulary;

import org.apache.ibatis.session.SqlSession;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import Vocabulary.Mapper.*;
import org.junit.jupiter.api.Test;

public class InsertDatabase {
    public static List<List<String>> processFile(String fileName) {
        List<List<String>> result = new ArrayList<>();

        try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] words = line.split("                 ");
                List<String> wordList = Arrays.asList(words);
                result.add(wordList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    public static void main(String[] args) {
        String fileName = "F:\\Desktop\\workspace\\crawler\\src\\main\\java\\Vocabulary\\Vocabulary.txt";
        List<List<String>> data = processFile(fileName);
        SqlSession sqlSession = MybatisUtils.getSqlSession();
       WordMapper wordMapper = sqlSession.getMapper(WordMapper.class);
        for (List<String> line : data) {
//            System.out.println(line.get(1));
            System.out.println(line.size());
            Word word=new Word(line.get(0),line.get(1));
            wordMapper.insert(word);
        }

    }
    @Test
    public void T(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        WordMapper wordMapper = sqlSession.getMapper(WordMapper.class);
        List<Word> words = wordMapper.getWords();
        System.out.println(words);
    }
}
